
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x15, 0xE1 };
IPAddress ip(10, 33, 50, 197); 
EthernetServer server(80);  // create a server at port 80 (default for some reason)

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();          
    pinMode(2, OUTPUT);       // set the LED to be an output
}

void loop()
{
    EthernetClient client = server.available();  // Gets a client that is connected to the server and has data available for reading.

    if (client) {  // got client?
        // every line ends with an \r\n, line is blank if we reach \n
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
        
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>ECE 387 Midterm Server LED Control</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>Server LED Control</h1>");
                    client.println("<p>Click to turn on/off.</p>");
                    client.println("<form method=\"get\">");
                    
                    if (HTTP_req.indexOf("BlueLED=2") > -1) {  // this checks to see if the checkbox on the server was clicked
                       // turn the LED's status to the opposite of what it currently is.
                      if (LED_status) {
                        LED_status = 0;
                      }
                      else {
                        LED_status = 1;
                      }
                    }
                    
                    if (LED_status) {    // switch LED on
                      digitalWrite(2, HIGH);
                      //checkbox is checked
                      client.println("<input type=\"checkbox\" name=\"BlueLED\" value=\"2\" \
                      onclick=\"submit();\" checked>BlueLED");
                      
                    }
                    else {              // switch LED off
                      digitalWrite(2, LOW);
                      //checkbox is unchecked
                      client.println("<input type=\"checkbox\" name=\"BlueLED\" value=\"2\" \
                      onclick=\"submit();\">BlueLED");
                    }
                    
                    
                    client.println("</form>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    currentLineIsBlank = false; 
                }
            } 
        } 
        delay(2);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } 
}

